import Vue from 'vue'
import App from './App'

Vue.config.productionTip = false
App.mpType = 'app'

// 封装的路由守卫
// import uniCrazyRouter from './components/crazy-router/index.js';
// Vue.use(uniCrazyRouter)
// uniCrazyRouter.beforeEach(async (to, from ,next)=>{
// 	const isLogin = uni.getStorageSync("accessToken") ? true : false;
// 	// 如果有token，或在其他不需要token的页面
// 	if (isLogin || to.url == 'pages/login/login' || to.url == 'pages/resister/resister' || to.url == 'pages/forget/forget') {
// 		next();
// 	}else{
// 		uniCrazyRouter.afterNotNext(() => {
// 			uni.navigateTo({url: '/pages/login/login'});
// 		})
// 	}
// })

// 注册全局popup组件
import uniPopup from './components/uni-popup/uni-popup.vue'
Vue.component('uni-popup', uniPopup)

//封装的请求
import Request from './components/luch-request/index.js'
const http = new Request();
const token = uni.getStorageSync('accessToken') || '';
http.setConfig((config) => { /* 设置全局配置 */
	// config.baseUrl = 'http://api.lxj.com/'
	config.baseUrl = 'https://api.tianjialixiangjia.com/'
	config.header = {
		...config.header,
		token: token,
		'content-type': 'application/x-www-form-urlencoded'
	}
	return config
})

// http.setConfig(config => {
// 	config.header = { ...config.header,
// 		accessToken: uni.getStorageSync('accessToken'),
// 	}
// 	return config
// })


// http.interceptor.request((config, cancel) => {
// 	config.header = { ...config.header,
// 		accessToken: uni.getStorageSync('accessToken'),
// 	}
// 	return config
// })

http.interceptor.response((response) => { /* 请求之后拦截器 */
console.log(uni.getStorageSync('accessToken')+"请求token")
	if (response.data.code === 998 || response.data.code === 999 || response.data.code === 996) { // 998登陆过期,996账户冻结
		uni.clearStorage();
		uni.clearStorageSync();

		uni.showToast({
			title: response.data.msg,
			icon: 'none'
		});
		response.config = {
			...response.header,
			token: "",
			'content-type': 'application/x-www-form-urlencoded'
		}
		http.config.header.token = "";
		console.log("998-------")
		console.log(http)
		console.log(http.config.header.token)
	}
	return response
}, (response) => { // 请求错误做点什么
	return response
})
console.log("200-------");
console.log(http);
console.log(http.config.header.token)

Vue.prototype.$http = http;

// 封装的url跳转
Vue.prototype.Router = function(url, type) {
	if (type == 'reLaunch') {
		uni.reLaunch({
			url: url
		});
	} else if (type == 'back') {
		uni.navigateBack();
	} else {
		uni.navigateTo({
			url: url
		});
	}
}

const app = new Vue({
	...App
})
app.$mount()
